// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-04-02 19:44
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE.segTree;

import org.junit.jupiter.api.Test;

/*
    树状数组实现日期订阅不重合 : 开点线段树实现
    线段树查找的是一段连续的区间
 */
class MyCalendar {

    class Node {
        boolean isBook = false;
        Node left, right;
        int start, end;

        public Node(int start, int end) {
            this.start = start;
            this.end = end;
        }
    }

    Node root;

    public MyCalendar() {

    }


    public boolean book(int start, int end) {
        return book(root, start, end);
    }

    public boolean book(Node root, int start, int end) {
        if (root == null) {
            this.root = new Node(start, end);
            return true;
        }
        if (root.start >= end) {
            if (root.left == null) {
                root.left = new Node(start, end);
                return true;
            }
            return book(root.left, start, end);
        }

        if (root.end <= start) {
            if (root.right == null) {
                root.right = new Node(start, end);
                return true;
            }
            return book(root.right, start, end);

        }
        return false;
    }

    @Test
    public void shout() {
        MyCalendar mc = new MyCalendar();
        System.out.println(mc.book(10, 20));
        System.out.println(mc.book(15, 25));
        System.out.println(mc.book(20, 30));
    }

}

/**
 * Your MyCalendar object will be instantiated and called as such:
 * MyCalendar obj = new MyCalendar();
 * boolean param_1 = obj.book(start,end);
 */